package com.yicong.ants.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.provider.Settings;
import androidx.fragment.app.Fragment;

import com.cchao.simplelib.core.Logs;
import com.cchao.simplelib.core.UiHelper;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class AppUtils {

    /**
     * 存放最新Apk的目录
     */
    public static final String APK_UPDATE_PATH = Environment.
        getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();

    // 下次是否提示更新标识 key
    private static final String IS_UPDATE = "IS_UPDATE";

    /**
     * 获取当前本地apk的版本
     */
    public static String getVersionName(Context mContext) {
        String versionName = null;
        try {
            //获取软件版本号，对应AndroidManifest.xml下android:versionCode
            versionName = mContext.getPackageManager().
                getPackageInfo(mContext.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }


    public static void setIsUpdate(Context context, boolean isUpdate) {
        SPUtils.put(context, IS_UPDATE, isUpdate);
    }

    public static boolean getIsUpdate(Context context) {
        return (boolean) SPUtils.get(context, IS_UPDATE, false);
    }

    /**
     * 跳转到App的权限设置页面
     */
    public static void goToAppSetting(Activity activity, Fragment fragment) {
        if (null != activity && null != fragment) {
            return;
        }
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        if (null != activity) {
            Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
            intent.setData(uri);
            activity.startActivityForResult(intent, ConstantCodeUtils.PERMISSION_REQUEST_CODE);
        } else {
            Uri uri = Uri.fromParts("package", fragment.getActivity().getPackageName(), null);
            intent.setData(uri);
            fragment.startActivityForResult(intent, ConstantCodeUtils.PERMISSION_REQUEST_CODE);
        }
    }

    /**
     * 判断是否安装目标应用
     *
     * @param packageName 目标应用安装后的包名
     * @return 是否已安装目标应用
     */
    public static boolean isInstallByread(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }

    public static void jumpToTel(Activity activity, String tel) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse(tel);
        intent.setData(data);
        IntentUtils.startActivity(intent, activity);
    }

    public static void jumpToWeb(Activity activity, String url) {
        try {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            Uri uri = Uri.parse(url);
            intent.setData(uri);
            activity.startActivity(intent);
        } catch (Exception e) {
            Logs.logException(e);
            UiHelper.showToast("链接异常");
            e.printStackTrace();
        }
    }

    public static String getAssetsString(Context context) {
        String str = "";
        try (InputStream inputStream = context.getAssets().open("agreement")) {
            int length = inputStream.available();
            byte[] bytes = new byte[length];
            int i = inputStream.read(bytes);
            str = new String(bytes, StandardCharsets.UTF_8).trim();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return str;
    }
}
